package edu.uwosh.cs342.group1.database;

import java.util.*;

import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;

import android.os.AsyncTask;

import edu.uwosh.cs342.group1.storage.*;

public class DbCloudHelper {
	/**
	 * Adds the user in the cloud database
	 * @param user
	 * @return
	 */
	public Boolean AddUser(CloudUser user) {
		AddUserTask task = new AddUserTask();
		user.setPassword(user.getPassword());
		task.execute(user);
		try {
			return task.get();
		}
		catch (Exception e) {
			return false;
		}
	}
	
	/**
	 * Updates the user in the cloud database
	 * @param user
	 * @param newPassword
	 * @return
	 */
	public Boolean UpdateUser(CloudUser user, String newPassword) {
		UpdateUserTask task = new UpdateUserTask();
		task.execute(user.getUsername(), user.getPassword(), newPassword);
		try {
			return task.get();
		}
		catch (Exception e) {
			return false;
		}
	}
	
	/**
	 * Authenticates the user from the cloud
	 * @param user
	 * @return
	 */
	public Boolean AuthenticateUser(CloudUser user) {
		SendForUserTask task = new SendForUserTask();
		user.setPassword(user.getPassword());
		boolean authed = false;
		task.execute(user);
		try {
			authed = task.get();
		}
		catch (Exception e) {
			user = null;
		}
		
		return authed;
	}
	
	/**
	 * Gets the list of available quizzes for the user
	 * @param username
	 * @return
	 */
	public List<Quiz> GetAvailableQuizzes(String username) {
		GetAllQuizzesTask task = new GetAllQuizzesTask();
		task.execute(username);
		try {
			return task.get();
		}
		catch (Exception e) {
			return null;
		}
	}
	
	/**
	 * Gets the quiz for the user, and decrypts the XML
	 * @param id
	 * @return
	 */
	public Quiz GetQuiz(int id) {
		GetQuizTask task = new GetQuizTask();
		task.execute(id);
		try {
			return task.get();
		}
		catch (Exception e) {
			return null;
		}
	}
	
	/**
	 * Submits the user's score
	 * @param score
	 * @return
	 */
	public Boolean SubmitScore(Scores score) {
		SubmitScoreTask task = new SubmitScoreTask();
		task.execute(score);
		try {
			return task.get();
		}
		catch (Exception e) {
			return false;
		}
	}
	
	/**
	 * Gets the top 10 user's scores
	 * @param username
	 * @return
	 */
	public List<Scores> GetUserScores(String username) {
		GetUserScoresTask task = new GetUserScoresTask();
		task.execute(username);
		try {
			return task.get();
		}
		catch (Exception e) {
			return null;
		}
	}
	
	/**
	 * Gets the top 10 scores for all users
	 * @return
	 */
	public List<Scores> GetAllScores() {
		GetAllScoresTask task = new GetAllScoresTask();
		task.execute();
		try {
			return task.get();
		}
		catch (Exception e) {
			return null;
		}
	}

	
	/**
	 * Adds a new user to the database
	 * @author Drew McCauley
	 *
	 */
	public class AddUserTask extends AsyncTask<CloudUser, Void, Boolean> {
		@Override
		protected Boolean doInBackground(CloudUser... params) {
			return new DbCloud().AddNewUser(params[0]);
		}
	}
	
	/**
	 * Updates the user's password in the database
	 * @author Drew McCauley
	 *
	 */
	public class UpdateUserTask extends AsyncTask<String, Void, Boolean> {
		@Override
		protected Boolean doInBackground(String... params) {
			return new DbCloud().UpdateUser(new CloudUser(params[0], params[1]), params[2]);
		}
	}

	/**
	 * Checks the user's authentication
	 * @author Drew McCauley
	 *
	 */
	private class SendForUserTask extends AsyncTask<CloudUser, Void, Boolean> {
		@Override
		protected Boolean doInBackground(CloudUser... params) {
			return new DbCloud().AuthenticateUser(params[0]);
		}
	}
	
	/**
	 * Gets all of the quizzes that the user has not taken
	 * @author Drew McCauley
	 *
	 */
	private class GetAllQuizzesTask extends AsyncTask<String, Void, List<Quiz>> {
		@Override
		protected List<Quiz> doInBackground(String... params) {
			return new DbCloud().GetAvailableQuizzes(params[0]);
		}
	}
	
	/**
	 * Gets a quiz from the database
	 * @author Drew McCauley
	 *
	 */
	private class GetQuizTask extends AsyncTask<Integer, Void, Quiz> {
		@Override
		protected Quiz doInBackground(Integer... params) {
			String xml = new DbCloud().GetQuiz(params[0]);
			Quiz quiz = null;
			if (xml != null) {
				Serializer serializer = new Persister();
				try {
					// quiz id will not be in the XML, it physically cannot be
					System.out.println(StringEncryptor.Decrypt(xml));
					quiz = serializer.read(Quiz.class, StringEncryptor.Decrypt(xml));
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				System.out.println(xml);
			}
			
			return quiz;
		}
	}
	
	/**
	 * Submits a quiz score to the main roster
	 * @author Drew McCauley
	 *
	 */
	private class SubmitScoreTask extends AsyncTask<Scores, Void, Boolean> {
		@Override
		protected Boolean doInBackground(Scores... params) {
			return new DbCloud().SubmitScore(params[0]);
		}
	}
	
	/**
	 * Gets the top 10 user's scores from the cloud
	 * @author Drew McCauley
	 *
	 */
	private class GetUserScoresTask extends AsyncTask<String, Void, List<Scores>> {
		@Override
		protected List<Scores> doInBackground(String... params) {
			return new DbCloud().GetUserScores(params[0]);
		}
	}
	
	/**
	 * Gets top 10 scores for all users from the cloud
	 * @author Drew McCauley
	 *
	 */
	private class GetAllScoresTask extends AsyncTask<String, Void, List<Scores>> {
		@Override
		protected List<Scores> doInBackground(String... params) {
			return new DbCloud().GetAllScores();
		}
	}
}
